package com.midea.thread;

import java.util.concurrent.*;

/**
 * @description：Callable的三种玩法
 * @author: wangwl23
 * @create: 2021-10-01 15:17
 */
public class ThreadDemo03 {

    /**
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Object> oneCallable = new Tickets<Object>();
        FutureTask<Object> oneTask = new FutureTask<Object>(oneCallable);
        //第一种通过Thread类
        Thread t = new Thread(oneTask);
        System.out.println(Thread.currentThread().getName());
        t.start();
        Object o = oneTask.get();
        //第二种FutureTask和ExecutorService
        ExecutorService es = Executors.newCachedThreadPool();
        es.submit(oneTask);
        Object o1 = oneTask.get();
        //第三种Callable和ExecutorService
        Future<Object> submit = es.submit(oneCallable);
        Object o2 = submit.get();
        es.shutdown();
        System.out.println(o);
        System.out.println(o1);
        System.out.println(o2);
    }

}

class Tickets<Object> implements Callable<Object>{
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"-->我是通过实现Callable接口通过FutureTask包装器来实现的线程");
        return (Object) "1";
    }
}
